package action;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;

import basic.PageInfo;
import basic.QuerySet;
import basic.ResultSet;
import basic.SortInfo;
import common.BaseAction;
import common.dict.CommonDict;
import entities.TBank;
import interfaces.IBankService;
import net.sf.json.JSONArray;
import util.ParameterUtil;
import utils.DateUtil;
import utils.ExcelUtil;
import utils.StringUtil;

public class BankAction extends BaseAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = 7507534255609348887L;

	private IBankService bankService;

	private TBank bankBean;
	private List<TBank> bankList;

	private String bnkNo;
	private String bnkNos;
	private String bnkName;
	private String bnkType;
	private String bnkEdc;
	private String bnkMer;
	private String bnkTer;
	private String bnkAuth;
	private String bnkKey;
	private String bnkBill;
	private String bnkPart;
	private String bnkFake;
	private Double bnkP0;
	private Double bnkR0;
	private Double bnkP1;
	private Double bnkR1;
	private Double bnkP2;
	private Double bnkR2;
	private Double bnkP3;
	private Double bnkR3;
	private Double bnkP4;
	private Double bnkR4;
	private Double bnkP5;
	private Double bnkR5;
	private String bnkState;
	private Date bnkDate;
	private Date bnkDateTop;
	private Date bnkDateEnd;
	private String bnkRemark;
	
	// 信息展示
	public void name() throws Exception {
		HttpServletResponse response = ServletActionContext.getResponse();
		response.setContentType("text/html;charset=UTF-8");
		if (!bankService.exist(bnkNo)) {
			response.getWriter().println("XXXXXX0000");
		} else {
			response.getWriter().println(bankService.fetch(bnkNo).getBnkName());
		}
	}

	public String show() throws Exception {
		if (!bankService.exist(bnkNo)) {
			addActionError("数据不存在");
			return INFO;
		}else{
			bankBean = bankService.fetch(bnkNo);
			return SUCCESS;
		}
	}

	public String edit() throws Exception {
		bankBean = new TBank();
		bankBean.setBnkPart("1");
		bankBean.setBnkFake("0");
		bankBean.setBnkState("1");
		return SUCCESS;
	}
	
	// 修改基本信息
	public String editinfo() throws Exception {
		if (!bankService.exist(bnkNo)) {
			addActionError("数据不存在");
			return INFO;
		}else{
			bankBean = bankService.fetch(bnkNo);
			return SUCCESS;
		}
	}
	
	// 修改溢价
	public String editmark() throws Exception {
		if (!bankService.exist(bnkNo)) {
			addActionError("数据不存在");
			return INFO;
		}else{
			bankBean = bankService.fetch(bnkNo);
			return SUCCESS;
		}
	}

	// 持久化对象
	public String save() throws Exception {
		if (bankService.exist(bnkNo)) {
			addActionError("编号已存在");
			return INFO;
		}
		bankBean = new TBank();
		bankBean.setBnkNo(bnkNo);
		bankBean.setBnkName(bnkName);
		bankBean.setBnkType(bnkType);
		bankBean.setBnkEdc(bnkEdc);
		bankBean.setBnkMer(bnkMer);
		bankBean.setBnkTer(bnkTer);
		bankBean.setBnkAuth(bnkAuth);
		bankBean.setBnkKey(bnkKey);
		bankBean.setBnkBill(bnkBill);
		bankBean.setBnkPart(bnkPart);
		bankBean.setBnkFake(bnkFake);
		bankBean.setBnkP0(4.5D);
		bankBean.setBnkR0(500D);
		bankBean.setBnkP1(4D);
		bankBean.setBnkR1(1000D);
		bankBean.setBnkP2(3.5D);
		bankBean.setBnkR2(1500D);
		bankBean.setBnkP3(3D);
		bankBean.setBnkR3(2000D);
		bankBean.setBnkP4(2.5D);
		bankBean.setBnkR4(2500D);
		bankBean.setBnkP5(2D);
		bankBean.setBnkR5(999999.99D);
		bankBean.setBnkState(bnkState);
		bankBean.setBnkDate(new Date());
		bankService.persist(bankBean);
		return SUCCESS;
	}
	
	// 保存基本信息
	public String saveinfo() throws Exception {
		if (!bankService.exist(bnkNo)) {
			addActionError("数据不存在");
			return INFO;
		}
		bankBean = new TBank();
		bankBean.setBnkNo(bnkNo);
		bankBean.setBnkName(bnkName);
		bankBean.setBnkType(bnkType);
		bankBean.setBnkEdc(bnkEdc);
		bankBean.setBnkMer(bnkMer);
		bankBean.setBnkTer(bnkTer);
		bankBean.setBnkAuth(bnkAuth);
		bankBean.setBnkKey(bnkKey);
		bankBean.setBnkBill(bnkBill);
		bankBean.setBnkPart(bnkPart);
		bankBean.setBnkFake(bnkFake);
		bankBean.setBnkState(bnkState);
		bankService.persist(bankBean);
		return SUCCESS;
	}
	
	// 保存MARKUP
	public String savemark() throws Exception {
		if (!bankService.exist(bnkNo)) {
			addActionError("数据不存在");
			return INFO;
		}
		bankBean = new TBank();
		bankBean.setBnkNo(bnkNo);
		bankBean.setBnkP0(bnkP0);
		bankBean.setBnkR0(bnkR0);
		bankBean.setBnkP1(bnkP1);
		bankBean.setBnkR1(bnkR1);
		bankBean.setBnkP2(bnkP2);
		bankBean.setBnkR2(bnkR2);
		bankBean.setBnkP3(bnkP3);
		bankBean.setBnkR3(bnkR3);
		bankBean.setBnkP4(bnkP4);
		bankBean.setBnkR4(bnkR4);
		bankBean.setBnkP5(bnkP5);
		bankBean.setBnkR5(bnkR5);
		bankService.persist(bankBean);
		return SUCCESS;
	}

	// AUTOCOMPLATE功能
	public void auto() throws Exception {
		QuerySet<Object> querySet = new QuerySet<Object>();

		HashMap<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("bnkNo", StringUtil.split(bnkNo));

		SortInfo sortInfo = new SortInfo();
		sortInfo.setSortField("bnkNo");
		sortInfo.setSortOrder(new Boolean(true));

		querySet.setPageInfo(new PageInfo(pageIndex, pageSize));
		querySet.setSortInfo(sortInfo);
		querySet.setParamMap(paramMap);

		ResultSet<TBank> resultSet = bankService.search(querySet);
		bankList = resultSet.getResultList();

		HttpServletResponse response = ServletActionContext.getResponse();
		response.setContentType("text/html;charset=UTF-8");
		response.getWriter().println(JSONArray.fromObject(bankList).toString());
	}

	// 列表展示页面
	// paramMap为查询条件,sortInfo为排序方式,pageInfo为分页对象
	// paramMap可为空,sortInfo可为空,pageInfo不能为空,如果需要查询全部则设置pageIndex为1,pageSize为Integer.MAX_VALUE
	public String list() throws Exception {
		QuerySet<Object> querySet = new QuerySet<Object>();

		HashMap<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("bnkNo", StringUtil.split(bnkNo));
		paramMap.put("bnkName", bnkName);
		paramMap.put("bnkType", bnkType);
		paramMap.put("bnkState", bnkState);
		paramMap.put("bnkDate", ParameterUtil.dateCombin(bnkDateTop, bnkDateEnd));

		SortInfo sortInfo = new SortInfo();
		sortInfo.setSortField((StringUtil.isEmpty(sortField)) ? "bnkNo" : sortField);
		sortInfo.setSortOrder(sortOrder);

		querySet.setPageInfo(new PageInfo(pageIndex, pageSize));
		querySet.setSortInfo(sortInfo);
		querySet.setParamMap(paramMap);

		ResultSet<TBank> resultSet = bankService.search(querySet);
		bankList = resultSet.getResultList();
		pageInfo = resultSet.getPageInfo();
		return SUCCESS;
	}
	
	public String listmark() throws Exception {
		QuerySet<Object> querySet = new QuerySet<Object>();

		HashMap<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("bnkNo", StringUtil.split(bnkNo));
		paramMap.put("bnkName", bnkName);
		paramMap.put("bnkType", bnkType);
		paramMap.put("bnkState", bnkState);
		paramMap.put("bnkDate", ParameterUtil.dateCombin(bnkDateTop, bnkDateEnd));

		SortInfo sortInfo = new SortInfo();
		sortInfo.setSortField((StringUtil.isEmpty(sortField)) ? "bnkNo" : sortField);
		sortInfo.setSortOrder(sortOrder);

		querySet.setPageInfo(new PageInfo(pageIndex, pageSize));
		querySet.setSortInfo(sortInfo);
		querySet.setParamMap(paramMap);

		ResultSet<TBank> resultSet = bankService.search(querySet);
		bankList = resultSet.getResultList();
		pageInfo = resultSet.getPageInfo();
		return SUCCESS;
	}

	// 导出数据
	// 查询条件等同于列表查询条件
	public String export() throws Exception {
		QuerySet<Object> querySet = new QuerySet<Object>();

		HashMap<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("bnkNo", StringUtil.split(bnkNo));
		paramMap.put("bnkName", bnkName);
		paramMap.put("bnkType", bnkType);
		paramMap.put("bnkState", bnkState);
		paramMap.put("bnkDate", ParameterUtil.dateCombin(bnkDateTop, bnkDateEnd));

		SortInfo sortInfo = new SortInfo();
		sortInfo.setSortField((StringUtil.isEmpty(sortField)) ? "bnkDate" : sortField);
		sortInfo.setSortOrder(sortOrder);

		querySet.setPageInfo(new PageInfo(1, CommonDict.EXPORT_LIMIT));
		querySet.setSortInfo(sortInfo);
		querySet.setParamMap(paramMap);

		ResultSet<TBank> resultSet = bankService.search(querySet);
		bankList = resultSet.getResultList();

		List<String> fieldHead = new ArrayList<String>();
		fieldHead.add("编号");
		fieldHead.add("银行名称");
		fieldHead.add("类型");
		fieldHead.add("关联商编");
		fieldHead.add("商编");
		fieldHead.add("终端号");
		fieldHead.add("授权号");
		fieldHead.add("key");
		fieldHead.add("账单名称");
		fieldHead.add("是否支持部分退款");
		fieldHead.add("一级溢价比例");
		fieldHead.add("一级区间范围");
		fieldHead.add("二级溢价比例");
		fieldHead.add("二级区间范围");
		fieldHead.add("三级溢价比例");
		fieldHead.add("三级区间范围");
		fieldHead.add("四级溢价比例");
		fieldHead.add("四级区间范围");
		fieldHead.add("五级溢价比例");
		fieldHead.add("五级区间范围");
		fieldHead.add("六级溢价比例");
		fieldHead.add("六级区间范围");
		fieldHead.add("状态");
		fieldHead.add("日期日期");
		fieldHead.add("日期时间");
		fieldHead.add("备注");

		List<List<String>> fieldData = new ArrayList<List<String>>();

		for (TBank bankBean : bankList) {
			List<String> fieldItem = new ArrayList<String>();
			fieldItem.add(bankBean.getBnkNo());
			fieldItem.add(bankBean.getBnkName());
			fieldItem.add(bankBean.getBnkType());
			fieldItem.add(bankBean.getBnkEdc());
			fieldItem.add(bankBean.getBnkMer());
			fieldItem.add(bankBean.getBnkTer());
			fieldItem.add(bankBean.getBnkAuth());
			fieldItem.add(bankBean.getBnkKey());
			fieldItem.add(bankBean.getBnkBill());
			fieldItem.add((bankBean.getBnkPart().equals("0")) ? "无效" : "有效");
			fieldItem.add(bankBean.getBnkP0().toString());
			fieldItem.add(bankBean.getBnkR0().toString());
			fieldItem.add(bankBean.getBnkP1().toString());
			fieldItem.add(bankBean.getBnkR1().toString());
			fieldItem.add(bankBean.getBnkP2().toString());
			fieldItem.add(bankBean.getBnkR2().toString());
			fieldItem.add(bankBean.getBnkP3().toString());
			fieldItem.add(bankBean.getBnkR3().toString());
			fieldItem.add(bankBean.getBnkP4().toString());
			fieldItem.add(bankBean.getBnkR4().toString());
			fieldItem.add(bankBean.getBnkP5().toString());
			fieldItem.add(bankBean.getBnkR5().toString());
			fieldItem.add((bankBean.getBnkState().equals("0")) ? "无效" : "有效");
			fieldItem.add(DateUtil.format(bankBean.getBnkDate(), DateUtil.DATE));
			fieldItem.add(DateUtil.format(bankBean.getBnkDate(), DateUtil.TIME));
			fieldItem.add(bankBean.getBnkRemark());

			fieldData.add(fieldItem);
		}

		inputStream = ExcelUtil.excel(fieldHead, fieldData);
		return SUCCESS;
	}

	////////////////////// ---------------------------//////////////////////////////////////////////////////////////////////////////////////////////////////////////
	////////////////////// ---------------------------//////////////////////////////////////////////////////////////////////////////////////////////////////////////
	////////////////////// ---------------------------//////////////////////////////////////////////////////////////////////////////////////////////////////////////

	public IBankService getBankService() {
		return bankService;
	}

	public void setBankService(IBankService bankService) {
		this.bankService = bankService;
	}

	public TBank getBankBean() {
		return bankBean;
	}

	public void setBankBean(TBank bankBean) {
		this.bankBean = bankBean;
	}

	public List<TBank> getBankList() {
		return bankList;
	}

	public void setBankList(List<TBank> bankList) {
		this.bankList = bankList;
	}

	public String getBnkNo() {
		return bnkNo;
	}

	public void setBnkNo(String bnkNo) {
		this.bnkNo = bnkNo;
	}

	public String getBnkNos() {
		return bnkNos;
	}

	public void setBnkNos(String bnkNos) {
		this.bnkNos = bnkNos;
	}

	public String getBnkName() {
		return bnkName;
	}

	public void setBnkName(String bnkName) {
		this.bnkName = bnkName;
	}

	public String getBnkType() {
		return bnkType;
	}

	public void setBnkType(String bnkType) {
		this.bnkType = bnkType;
	}

	public String getBnkEdc() {
		return bnkEdc;
	}

	public void setBnkEdc(String bnkEdc) {
		this.bnkEdc = bnkEdc;
	}

	public String getBnkMer() {
		return bnkMer;
	}

	public void setBnkMer(String bnkMer) {
		this.bnkMer = bnkMer;
	}

	public String getBnkTer() {
		return bnkTer;
	}

	public void setBnkTer(String bnkTer) {
		this.bnkTer = bnkTer;
	}

	public String getBnkAuth() {
		return bnkAuth;
	}

	public void setBnkAuth(String bnkAuth) {
		this.bnkAuth = bnkAuth;
	}

	public String getBnkKey() {
		return bnkKey;
	}

	public void setBnkKey(String bnkKey) {
		this.bnkKey = bnkKey;
	}

	public String getBnkBill() {
		return bnkBill;
	}

	public void setBnkBill(String bnkBill) {
		this.bnkBill = bnkBill;
	}

	public String getBnkPart() {
		return bnkPart;
	}

	public void setBnkPart(String bnkPart) {
		this.bnkPart = bnkPart;
	}

	public Double getBnkP0() {
		return bnkP0;
	}

	public void setBnkP0(Double bnkP0) {
		this.bnkP0 = bnkP0;
	}

	public Double getBnkR0() {
		return bnkR0;
	}

	public void setBnkR0(Double bnkR0) {
		this.bnkR0 = bnkR0;
	}

	public Double getBnkP1() {
		return bnkP1;
	}

	public void setBnkP1(Double bnkP1) {
		this.bnkP1 = bnkP1;
	}

	public Double getBnkR1() {
		return bnkR1;
	}

	public void setBnkR1(Double bnkR1) {
		this.bnkR1 = bnkR1;
	}

	public Double getBnkP2() {
		return bnkP2;
	}

	public void setBnkP2(Double bnkP2) {
		this.bnkP2 = bnkP2;
	}

	public Double getBnkR2() {
		return bnkR2;
	}

	public void setBnkR2(Double bnkR2) {
		this.bnkR2 = bnkR2;
	}

	public Double getBnkP3() {
		return bnkP3;
	}

	public void setBnkP3(Double bnkP3) {
		this.bnkP3 = bnkP3;
	}

	public Double getBnkR3() {
		return bnkR3;
	}

	public void setBnkR3(Double bnkR3) {
		this.bnkR3 = bnkR3;
	}

	public Double getBnkP4() {
		return bnkP4;
	}

	public void setBnkP4(Double bnkP4) {
		this.bnkP4 = bnkP4;
	}

	public Double getBnkR4() {
		return bnkR4;
	}

	public void setBnkR4(Double bnkR4) {
		this.bnkR4 = bnkR4;
	}

	public Double getBnkP5() {
		return bnkP5;
	}

	public void setBnkP5(Double bnkP5) {
		this.bnkP5 = bnkP5;
	}

	public Double getBnkR5() {
		return bnkR5;
	}

	public void setBnkR5(Double bnkR5) {
		this.bnkR5 = bnkR5;
	}

	public String getBnkState() {
		return bnkState;
	}

	public void setBnkState(String bnkState) {
		this.bnkState = bnkState;
	}

	public Date getBnkDate() {
		return bnkDate;
	}

	public void setBnkDate(Date bnkDate) {
		this.bnkDate = bnkDate;
	}

	public Date getBnkDateTop() {
		return bnkDateTop;
	}

	public void setBnkDateTop(Date bnkDateTop) {
		this.bnkDateTop = bnkDateTop;
	}

	public Date getBnkDateEnd() {
		return bnkDateEnd;
	}

	public void setBnkDateEnd(Date bnkDateEnd) {
		this.bnkDateEnd = bnkDateEnd;
	}

	public String getBnkRemark() {
		return bnkRemark;
	}

	public void setBnkRemark(String bnkRemark) {
		this.bnkRemark = bnkRemark;
	}

	public String getBnkFake() {
		return bnkFake;
	}

	public void setBnkFake(String bnkFake) {
		this.bnkFake = bnkFake;
	}
	
	

}
